added SSCLI 1.0
[windows-sources.git] / shared source / sscli20 / jscript / engine / typeof.cs
blob2e778430a39af3d1a45a9d996178b01fd4704962
1 // ==++==
2 //
3 //
4 // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
5 //
6 // The use and distribution terms for this software are contained in the file
7 // named license.txt, which can be found in the root of this distribution.
8 // By using this software in any fashion, you are agreeing to be bound by the
9 // terms of this license.
10 //
11 // You must not remove this notice, or any other, from this software.
12 //
13 //
14 // ==--==
16 namespace Microsoft.JScript {
18 using Microsoft.JScript.Vsa;
19 using System;
20 using System.Reflection;
21 using System.Reflection.Emit;
23 public sealed class Typeof : UnaryOp{
24 internal Typeof(Context context, AST operand)
25 : base(context, operand) {
28 internal override Object Evaluate(){
29 try{
30 return JScriptTypeof(this.operand.Evaluate(), VsaEngine.executeForJSEE);
31 }catch(JScriptException e){
32 if ((e.Number & 0xFFFF) == (int)JSError.UndefinedIdentifier)
33 return "undefined";
34 throw e;
38 internal override IReflect InferType(JSField inference_target){
39 return Typeob.String;
42 public static String JScriptTypeof(Object value){
43 return JScriptTypeof(value, false);
46 internal static String JScriptTypeof(Object value, bool checkForDebuggerObject){
47 switch (Convert.GetTypeCode(value)){
48 case TypeCode.Empty:
49 return "undefined";
50 case TypeCode.DBNull:
51 return "object";
52 case TypeCode.Object:
53 if (value is Missing || value is System.Reflection.Missing) return "undefined";
54 return value is ScriptFunction ? "function" : "object";
55 case TypeCode.Boolean:
56 return "boolean";
57 case TypeCode.Char:
58 case TypeCode.String:
59 return "string";
60 case TypeCode.SByte:
61 case TypeCode.Byte:
62 case TypeCode.Int16:
63 case TypeCode.UInt16:
64 case TypeCode.Int32:
65 case TypeCode.UInt32:
66 case TypeCode.Int64:
67 case TypeCode.UInt64:
68 case TypeCode.Single:
69 case TypeCode.Double:
70 case TypeCode.Decimal:
71 return "number";
72 case TypeCode.DateTime:
73 return "date";
75 return "unknown";
78 internal override void TranslateToIL(ILGenerator il, Type rtype){
79 if (this.operand is Binding)
80 //Make sure that no exception is thrown if the operand is an undefined identifier
81 ((Binding)this.operand).TranslateToIL(il, Typeob.Object, true);
82 else
83 this.operand.TranslateToIL(il, Typeob.Object);
84 il.Emit(OpCodes.Call, CompilerGlobals.jScriptTypeofMethod);
85 Convert.Emit(this, il, Typeob.String, rtype);